package top.heue.test.fragment.mvi

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import top.heue.test.databinding.FragmentMviBinding
import top.heue.utils.AD
import top.heue.utils.base.base.BaseFragment
import top.heue.utils.base.helper.inflate
import top.heue.utils.lifecycle.helper.viewModel
import top.heue.utils.log.L

class MVIFragment : BaseFragment() {
    private val mviModel: TestMVIModel by viewModel()
    private val bind: FragmentMviBinding by inflate {
        mvi = mviModel
        mviModel.onState(viewLifecycleOwner) {
            L.d(this)
            when(this.state1) {
                is State1.Loading -> {
                    L.d("加载中...")
                    AD(activity) {
                        title("弹出对话框")
                        message("测试MD对话框效果，这个对话框只状态有改变时弹出")
                        positive("确定") {_,_->
                            L.w("点击了确定")
                        }
                    }.show()
                }

                State1.Init -> {}
            }
        }
        mviModel.onEffect(viewLifecycleOwner, {TestUIEffect.ShowDialogState}) {
            AD(activity) {
                title("弹出对话框")
                message("测试MD对话框效果，这个对话框每次都弹出")
                input("a","第一个")
                input("b", "第二个")
                positive("确定") {_,_->
                    L.w("点击了确定")
                }
            }.show()
        }
        mviModel.onState(viewLifecycleOwner, {state1}) {
            when(this) {
                is State1.Loading ->{
                    L.w(this)
                }

                else->{}
            }
        }
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        return bind.root
    }
}